package it.univaq.di.helpdesk.business.common.impl.ibatis;


import it.univaq.di.helpdesk.business.common.TicketService;
import it.univaq.di.helpdesk.business.errors.BusinessException;
import it.univaq.di.helpdesk.business.model.Attachment;
import it.univaq.di.helpdesk.business.model.AttachmentData;
import it.univaq.di.helpdesk.business.model.Category;
import it.univaq.di.helpdesk.business.model.Comment;
import it.univaq.di.helpdesk.business.model.Priority;
import it.univaq.di.helpdesk.business.model.Status;
import it.univaq.di.helpdesk.business.model.Ticket;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.ibatis.sqlmap.client.SqlMapClient;

public class IBATISTicketService implements TicketService {
	private SqlMapClient sqlMap;

	public IBATISTicketService(SqlMapClient sqlMap) {
		super();
		this.sqlMap = sqlMap;
	}

	@Override
	public void addTicket(Ticket tiket) throws BusinessException {
		try {
			sqlMap.startTransaction();
			sqlMap.startBatch();
			
			sqlMap.insert("addTicket", tiket);
			
			for(Attachment attachment : tiket.getAttachment()) {
				attachment.setTicketID(tiket.getTicketID());
				AttachmentData attachmentData = attachment.getAttachmentData();
				sqlMap.insert("addAttachmentData", attachmentData);
				sqlMap.insert("addAttachment", attachment);
			}

			sqlMap.executeBatch();
			sqlMap.commitTransaction();
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		} finally {
			  try {
				sqlMap.endTransaction();
			} catch (SQLException e) {}
		}
	}

	@Override
	public Ticket findTicketById(int ID) throws BusinessException {
		Ticket ticket = null;
		
		try {
			ticket = (Ticket) sqlMap.queryForObject("findTicketById", ID);
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
		return ticket;
	}
	
	@Override
	public void addComment(Comment comment) throws BusinessException {
		try {
			sqlMap.insert("addComment", comment);
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
	}
	
	@Override
	public List<Category> findSubCatgories(int parentID) throws BusinessException {
		List<Category> categories = null;

		try {
			categories = (List<Category>) sqlMap.queryForList("getSubCategoriesByParent", parentID);
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
		return categories;
	}

	@Override
	public List<Category> findParentCategories() throws BusinessException {
		List<Category> categories = null;

		try {
			categories = (List<Category>) sqlMap.queryForList("getParentCategories");
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
		return categories;
	}

	@Override
	public List<Priority> findPriorities() throws BusinessException {
		List<Priority> priorities = null;
		
		try {
			priorities = (List<Priority>) sqlMap.queryForList("getPriorities");
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
		return priorities;
	}

	@Override
	public List<Status> findStatus() throws BusinessException {
		List<Status> status = null;
		
		try {
			status = (List<Status>) sqlMap.queryForList("getAllStatus");
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
		return status;
	}

	@Override
	public List<Comment> findCommentsByTicketId(int ticketID)
			throws BusinessException {
		List<Comment> comments = null;
		
		try {
			comments = (List<Comment>) sqlMap.queryForList("Comments.getCommentByTicketID", ticketID);
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
		
		return comments;
	}

	@Override
	public Attachment getAttachmentById(int attachmentID)
			throws BusinessException {
		Attachment attachment = null;
		
		try {
			attachment = (Attachment) sqlMap.queryForObject("getAttachmentByID",attachmentID);
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
		
		return attachment;
	}

	@Override
	public List<Ticket> findTicketList(Map<String, String> queryParam)
			throws BusinessException {
		List<Ticket> tickets = null;

		try {
			tickets = (List<Ticket>) sqlMap.queryForList("findTicketList", queryParam);
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
		
		return tickets;
	}

	@Override
	public void addAttachment(Attachment attachment) throws BusinessException {
		try {
			sqlMap.startTransaction();
			sqlMap.startBatch();
			
			AttachmentData attachmentData = attachment.getAttachmentData();
			sqlMap.insert("addAttachmentData", attachmentData);
			sqlMap.insert("addAttachment", attachment);

			sqlMap.executeBatch();
			sqlMap.commitTransaction();
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		} finally {
			  try {
				sqlMap.endTransaction();
			} catch (SQLException e) {}
		}
	}
	
	@Override
	public void changeTicketStatus(Map<String, String> mapstatus) throws BusinessException {
		try {
						
			sqlMap.update("changeTicketStatus", mapstatus);
			
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		} 
	}
	
	@Override
	public void changeTicketPriority(Map<String, String> mappriority) throws BusinessException {
		try {
			
			sqlMap.update("changeTicketPriority", mappriority);
			
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		} 
	}

}
